<?php

namespace app\controller\backed;

use app\BaseController;
use app\model\Event as EventModel;
use app\service\DingNotice as DingNoticeService;
use app\model\DingUser as DingUserModel;

class Event extends BaseController
{

    public function getEvents()
    {
        $params = request()->param();

        $pageNo = isset($params['pageNo']) ? $params['pageNo'] : 1;
        $pageSize = isset($params['pageSize']) ? $params['pageSize'] : 20;

        $res = EventModel::where('user_id = '.$params['uid'])->paginate(
            $pageSize, false, [
            'page' => $pageNo
        ]);

        return $this->successMessage('查询成功',$res);
    }

    public function createEvent()
    {       

        $params = request()->param();
        $get_time = date('Y-m-d H:i:s',strtotime($params['get_time']));

        $data = [
        	'title' => $params['title'],
			'desc' => $params['desc'],
			'get_time' => $get_time,
            'user_id' => $params['uid'],
        ];

        $eventModel = new EventModel();
        $eventModel->save($data);

        $ding_params = [
            "title" =>  'HR Online消息通知',
            "text" => "您好，在".$get_time."新增一条新事件".$params['title'],
        ];

        $user = DingUserModel::find($params['uid']);
        if($user){
            $dingNoticeService = new DingNoticeService;
            $dingNoticeService->companyMsg($user['id'],'markdown',$ding_params);
        }

        return $this->successMessage('创建成功');

    }


    public function deleteEvent()
    {

        $params = request()->param();
        EventModel::destroy($params['id']);
        
        return $this->successMessage('删除成功');
    }


}